﻿Public Class RightAlignedPopupOffsetConverter
    Inherits Markup.MarkupExtension
    Implements IValueConverter

    Public Sub New()
    End Sub

    Private Shared mConverter As RightAlignedPopupOffsetConverter
    Public Overrides Function ProvideValue(serviceProvider As IServiceProvider) As Object
        If mConverter Is Nothing Then
            mConverter = New RightAlignedPopupOffsetConverter
        End If
        Return mConverter
    End Function

    Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
        Dim popup As Primitives.Popup = CType(value, Primitives.Popup)
        Dim grid As Grid = WpfUtil.FindAncestorOfType(Of Grid)(popup)
        Return grid.ActualWidth - popup.ActualWidth
    End Function

    Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
        Throw New NotImplementedException
    End Function

End Class
